# Stage 1: Base Environment Setup
FROM ubuntu:jammy

# Metadata
LABEL author="zhangjianshe@gmail.com"
LABEL usage="Development environment for compiling and packaging Java/Node projects."
ENV DEBIAN_FRONTEND=noninteractive
# Set locale and timezone
# Install 'tzdata' to ensure the timezone link works, then set the timezone.
ENV LANG=C.UTF-8
RUN apt-get update && apt-get install -y tzdata \
    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

RUN apt-get install -y curl apt-utils

# 1. Add NodeSource PPA for a stable Node.js/NPM version (Node 20 LTS)
# 'curl' is installed in the next step, but it is often available or can be replaced with 'wget' if needed.
# Since 'curl' is in the main install list, this assumes it can be run after the list is processed
# or that the base image provides a minimal downloader.
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash -

# 2. Install core development tools, JDK 11, and Maven
RUN apt-get install -y \
    software-properties-common \
    curl \
    git \
    nodejs \
    pv \
    # Install JDK 11 and Maven from Ubuntu repositories
    openjdk-11-jdk \
    maven \
    # Clean up APT cache to significantly reduce image size
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 3. Configure NPM registry mirror for faster package downloads
RUN npm config set registry https://registry.npmmirror.com

# 4. Configure Maven Local Repository Path
# 4a. Create the target repository directory, which matches the volume mount.
RUN mkdir -p /repository/java \
    # 4b. Create a custom settings.xml to define the local repository path.
    && echo '<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">' > /usr/share/maven/conf/settings.xml \
    && echo '  <localRepository>/repository/java</localRepository>' >> /usr/share/maven/conf/settings.xml \
    && echo '</settings>' >> /usr/share/maven/conf/settings.xml

# 4. Set environment variables
# Define JAVA_HOME and MAVEN_HOME based on standard APT install locations for compatibility.
ENV MAVEN_HOME="/usr/share/maven"
ENV JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
# Prepend tool bins to PATH
ENV PATH="$MAVEN_HOME/bin:$JAVA_HOME/bin:$PATH"

# 5. Copy and configure entrypoint script
# Set executable permission (+x or 755) directly during the copy
COPY  start.sh /start.sh
RUN chmod 755 /start.sh

# 6. Define working directory and volumes
ENV WORKDIR="/source"
WORKDIR ${WORKDIR}
# Define volume mount points for source code and local repository cache
VOLUME ["/source", "/repository"]

# Final execution command
ENTRYPOINT ["/start.sh"]